package com.example.springboot.controller;


import com.example.springboot.pojo.Building;
import com.example.springboot.pojo.Result;
import com.example.springboot.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @GetMapping("/getAllBuilding")
    public Result<?> getAllBuilding(){
        List<Building> list = buildingService.getAllBuilding();
        return Result.success(list);
    }

    @PostMapping("/addBuilding")
    public Result<?> addBuilding(String building,Integer unitCount,Integer floorCount,Integer roomCount){
//        System.out.println(unitCount);
        try{
            if(buildingService.findBuilding(building) != null){
                return Result.error("此楼栋信息已经存在，不可再次添加");
            }
            buildingService.addBuilding(building,unitCount,floorCount,roomCount);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }

    @PutMapping("/deleteBuilding")
    public Result<?> deleteBuilding(String building){
        try{
            buildingService.deleteBuilding(building);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }

    @PostMapping("/updateBuilding")
    public Result<?> updateBuilding(String building,Integer unitCount,Integer floorCount,Integer roomCount){
        try{
            buildingService.updateBuilding(building,unitCount,floorCount,roomCount);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }
}
